package com.xx.job.common.util;

import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import nl.bitwalker.useragentutils.Version;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Locale;

public class OsAndBrowserInfoUtil {

    private UserAgent userAgent;
    public OsAndBrowserInfoUtil(){

    }

    public OsAndBrowserInfoUtil(HttpServletRequest request){
        this.userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));  //WINDOWS-CHROME
    }

    /**
     * 获取操作系统,浏览器及浏览器版本信息
     * @param request
     * @return
     */
    public static String getOsAndBrowserInfo(HttpServletRequest request){
        UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));  //WINDOWS-CHROME
        //获取浏览器信息
        Browser browser = userAgent.getBrowser();
        //获取浏览器版本
        Version version = userAgent.getBrowserVersion();
        //获取浏览器所有语言
        String languages = request.getHeader("Accept-Language");
        //获取操作系统
        OperatingSystem os = userAgent.getOperatingSystem();    //WINDOWS
        //获取ip地址
        String ip = "";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();   //192.168.50.34
        }catch (UnknownHostException e){
            e.printStackTrace();
        }

        //使用locale获取
        //本地默认语言-系统设定语言+国家
        Locale locale = Locale.getDefault();
        //本地默认语言-系统设定语言
        String language = locale.getLanguage();
        //本地默认国家-系统设定国家
        String country = locale.getCountry();
        return "";
    }



    //获取浏览器信息
    public String getBrowser(){
        return userAgent.getBrowser().getName() + userAgent.getBrowserVersion();
    }

    //获取操作系统
    public String getOs(){
        return userAgent.getOperatingSystem().getName();
    }

    public String getIp(){
        //获取ip地址
        String ip = "";
        try {
            ip = InetAddress.getLocalHost().getHostAddress();   //192.168.50.34
        }catch (UnknownHostException e){
            e.printStackTrace();
        }
        return ip;
    }

}
